import axios from 'axios';
import FormData from 'form-data';
import yaml from 'js-yaml';  
import fs from 'fs';
import path from 'path';
import { AppConfig } from "../config/app.config"

interface LatestFile{
  url:string;
  sha512:string;
  size:number
}

interface LatestYml {
  version:string;
  files : Array<LatestFile>
  path:string;
  sha512:string;
  releaseDate:string;
  [key : string] : any
}

const latestConfig  = yaml.load(fs.readFileSync(path.resolve('build/latest.yml'), 'utf8')) as LatestYml;

const fileUrl:string = path.resolve('build/' + latestConfig.files[0].url) 

const upLoadUrl:string = AppConfig.SERVICE_URL + AppConfig.UPDATE_BASE_API


async function uploadAppPackage(url:string,filePath:string) {  
  try {  
    const formData = new FormData();  
    formData.append('exeFile', fs.createReadStream(filePath), {  
      filename: filePath,  
      contentType: 'application/octet-stream', // 对于二进制文件，通常使用此类型  
    });  
  
    await axios.post(url, formData, {  
      headers: formData.getHeaders(),  
    });  
  
    console.log('文件已上传至私人服务器');  

  } catch (error) {  
    console.error('Upload failed:', error.message);  
  }  
}  
  
  
uploadAppPackage(upLoadUrl,fileUrl )
